#include <http_server.h>

extern CloudApi *cloud_api;
string ptree_to_string(ptree &pt)
{
    //return pt;
    stringstream ss;
    //write_json(ss, pt);
    write_json_fix(ss, pt);
    return (ss.str());
}
void set_succeed_return(ptree &pt)
{
    pt.put<string>("data","", string_translator<std::string>());
    pt.put<string>("errorCode","", string_translator<std::string>());
    pt.put<string>("msg","successed", string_translator<std::string>());
    pt.put<bool>("successed",true);
}
void set_error_return(ptree &pt)
{
    pt.put<string>("data","", string_translator<std::string>());
    pt.put<string>("errorCode","", string_translator<std::string>());
    pt.put<string>("msg","error", string_translator<std::string>());
    pt.put<bool>("successed",false);
}
//get_id
ptree getMac()
{
    ptree id;
	int sockfd;
	struct ifreq tmp;
	char macAddr[20];
	sockfd=socket(AF_INET,SOCK_STREAM,0);
	if(sockfd<0){
		cout<<"create socket fail"<<endl;
	}
	memset(&tmp,0,sizeof(struct ifreq));
	strncpy(tmp.ifr_name,"eno1",sizeof(tmp.ifr_name)-1);
	if((ioctl(sockfd,SIOCGIFHWADDR,&tmp))<0){
		cout<<"mac ioctl error"<<endl;
	}

	sprintf(macAddr,"%02x:%02x:%02x:%02x:%02x:%02x",
			(unsigned char)tmp.ifr_hwaddr.sa_data[0],
			(unsigned char)tmp.ifr_hwaddr.sa_data[1],
			(unsigned char)tmp.ifr_hwaddr.sa_data[2],
			(unsigned char)tmp.ifr_hwaddr.sa_data[3],
			(unsigned char)tmp.ifr_hwaddr.sa_data[4],
			(unsigned char)tmp.ifr_hwaddr.sa_data[5]
		);
	close(sockfd);
	string mac_address = macAddr;
    id.put<string>("data",mac_address,string_translator<std::string>());
    id.put<string>("errorCode","", string_translator<std::string>());
	id.put<string>("msg","successed", string_translator<std::string>());
	id.put<bool>("successed",true);
	return id;
}

// get map png
bool get_map_png(string mapUri, struct evbuffer *databuf,int &code, string &reason)
{
    int fd = open(mapUri.c_str(), O_RDONLY);    // read only
    ROS_INFO("file:%s",mapUri.c_str());
    if (fd > 0)
    {
        int length = lseek(fd, 0, SEEK_END);
        int ret = evbuffer_add_file(databuf, fd, 0, length);
        if (ret == 0)
        {
            return true;
        }
    }
    close(fd);
    code = 404;
    reason = "failed";
    return false;
}
// get map pcd
bool get_map_pcd(string mapUri, struct evbuffer *databuf,int &code, string &reason)
{
    int fd = open(mapUri.c_str(), O_RDONLY);    // read only
    if (fd > 0)
    {
        int length = lseek(fd, 0, SEEK_END);
        int ret = evbuffer_add_file(databuf, fd, 0, length);
        if (ret == 0)
        {
            return true;
        }
    }
    close(fd);
    code = 404;
    reason = "failed";
    return false;
}
// get map pgm
bool get_map_pgm(string mapUri, struct evbuffer *databuf,int &code, string &reason)
{
    int fd = open(mapUri.c_str(), O_RDONLY);    // read only
    if (fd > 0)
    {
        int length = lseek(fd, 0, SEEK_END);
        int ret = evbuffer_add_file(databuf, fd, 0, length);
        if (ret == 0)
        {
            return true;
        }
    }
    close(fd);
    code = 404;
    reason = "failed";
    return false;
}
//set param
ptree set_param(char buf[])
{
    ROS_INFO("post message:%s",buf);
    ptree param;
    return param;
}

//get param
ptree get_param(string map_name)
{
	ptree param;

	return param;
}
//add navi point
ptree add_navi_point(char buf[])
{
    ROS_INFO("buf:%s",buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
//delete navi point
ptree del_navi_point(string map_name, string point_name)
{
    ROS_INFO("map_name:%s,point_name:%s",map_name.c_str(),point_name.c_str());
    ptree info;
    set_succeed_return(info);
    return info;
}

//rename navi point
ptree rename_navi_point(char buf[])
{
    ROS_INFO("buf:%s",buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
//get point
string get_navi_point(string map_name)
{
    return cloud_api->get_navi_point(map_name);
}
string get_init_point(string map_name)
{
    return cloud_api->get_init_point(map_name);
}
string get_calibration_point(string map_name)
{
    return cloud_api->get_calibration_point(map_name);
}
string get_charge_point(string map_name)
{
    return cloud_api->get_charge_point(map_name);
}
string get_work_point(string map_name)
{
    return cloud_api->get_work_point(map_name);
}
string get_user_point(string map_name)
{
    return cloud_api->get_user_point(map_name);
}

//get path
string get_navi_path(string map_name)
{
    return cloud_api->get_navi_path(map_name);
}
string get_virtual_wall(string map_name)
{
    return cloud_api->get_virtual_wall(map_name);
}
string get_user_path(string map_name)
{
    return cloud_api->get_user_path(map_name);
}

string get_virtual_wall_point(string map_name)
{
    return cloud_api->get_virtual_wall_point(map_name);
}
string get_user_path_point(string map_name)
{
    return cloud_api->get_user_path_point(map_name);
}

//get area
string get_slow_area(string map_name)
{
    return cloud_api->get_slow_area(map_name);
}
string get_stop_area(string map_name)
{
    return cloud_api->get_stop_area(map_name);
}
string get_user_area(string map_name)
{
    return cloud_api->get_user_area(map_name);
}
string get_slow_area_point(string map_name)
{
    return cloud_api->get_slow_area_point(map_name);
}
string get_stop_area_point(string map_name)
{
    return cloud_api->get_stop_area_point(map_name);
}
string get_user_area_point(string map_name)
{
    return cloud_api->get_user_area_point(map_name);
}

//add navi path
ptree add_navi_path(char buf[])
{
    ROS_INFO("buf:%s",buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
//delete navi point
ptree del_navi_path(string map_name, string path_name)
{
    ROS_INFO("map_name:%s,path_name:%s",map_name.c_str(),path_name.c_str());
    ptree info;
    set_succeed_return(info);
    return info;
}

//get task
ptree get_task()
{
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree add_task(char buf[])
{
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree edit_task(char buf[])
{
    ptree info;
    set_succeed_return(info);
    return info;
}

//delete navi point
ptree del_task(string map_name, string path_name)
{
    ROS_INFO("map_name:%s,path_name:%s",map_name.c_str(),path_name.c_str());
    ptree info;
    set_succeed_return(info);
    return info;
}

//get_default_task
ptree get_default_task(string map_name)
{
    ROS_INFO("map_name:%s",map_name.c_str());
    ptree info;
    set_succeed_return(info);
    return info;
}

//set default task
ptree set_default_task(char buf[])
{
    ROS_INFO("buf:%s",buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

//del_default_task
ptree del_default_task(string map_name)
{
    ROS_INFO("map_name:%s",map_name.c_str());
    ptree info;
    set_succeed_return(info);
    return info;
}

//set head mode
ptree set_head_mode(string cmd)
{
    ROS_INFO("cmd:%s",cmd.c_str());
    ptree info;
    set_succeed_return(info);
    return info;
}

//set charge mode
ptree set_charge_cmd(string cmd)
{
    ROS_INFO("cmd:%s",cmd.c_str());
    ptree info;
    set_succeed_return(info);
    return info;
}

//set manual mode
ptree set_manual_mode(string cmd)
{
    ROS_INFO("cmd:%s",cmd.c_str());
    ptree info;
    set_succeed_return(info);
    return info;
}

//get status
ptree get_status()
{
    ptree info;
    set_succeed_return(info);
    return info;
}

//get gps data
ptree get_gps_data()
{
    ptree info;
    set_succeed_return(info);
    return info;
}

//get alarm
ptree get_alarm(string map_name)
{
    ptree info;
    set_succeed_return(info);
    return info;
}

//get log
ptree get_log(string map_name)
{
    ptree info;
    set_succeed_return(info);
    return info;
}

// used by local server
// load map

ptree edit_map(string map_name)
{
    bool isOk = cloud_api->edit_map(map_name);
    ptree info;
    if(isOk)
      set_succeed_return(info);
    else
      set_error_return(info);
    return info;
}

string load_map(string map_name)
{
    string load = cloud_api->load_map(map_name);
    return load;
}
ptree get_load_map_status()
{
    ptree status = cloud_api->get_load_map_status();
    return status;
}
ptree set_navi_speed(string level)
{
    cloud_api->set_navi_speed(atoi(level.c_str()));
    ptree info;
    set_succeed_return(info);
    return info;
}

// init position
ptree init_localization(string map_name, string init_point_name)
{
    cloud_api->initialize_localizaition(map_name,init_point_name);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree get_init_status()
{
    ptree init_status = cloud_api->get_initialize_status();
    return init_status;
}

ptree update_navi_info()
{
    cloud_api->update_navi_info();
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree start_task(char buf[])
{
    cloud_api->start_task(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree stop_task()
{
    cloud_api->stop_task();
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree get_task_status()
{
    ptree task_status = cloud_api->get_task_status();
    return task_status;
}

//set move cmd
ptree set_move_cmd(char buf[])
{
    cloud_api->set_move_cmd(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree navi_to_point(char buf[])
{
    cloud_api->navi_to_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree navi_to_dest(char buf[])
{
    cloud_api->navi_to_dest(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree navi_along_path(char buf[])
{
    cloud_api->navi_along_path(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

//add by client Genxin
ptree connect()
{
    ptree connect = cloud_api->get_basic_info();
    return connect;
}

// get map list 
string get_map_list()
{
	string map_list = cloud_api->get_map_list();
	return map_list;
}

ptree del_map(string map_name)
{
    ptree info;
    if(cloud_api->del_map(map_name))
        set_succeed_return(info);
    else
        set_error_return(info);
    return info;
}

ptree set_control_mode(string mode)
{
    cloud_api->set_control_mode(mode);

    ptree info;
    set_succeed_return(info);

	return info;
}

ptree download_file(char buf[])
{
    ptree file;
    if(cloud_api->download_file(buf))
    {
        set_succeed_return(file);
    }
    else
    {
        file.put<string>("data","", string_translator<std::string>());
        file.put<string>("errorCode","", string_translator<std::string>());
        file.put<string>("msg","failed", string_translator<std::string>());
        file.put<bool>("successed",false);
    }
    return file;
}

ptree get_3d_lidar()
{
    ptree lidar_3d = cloud_api->get_3d_lidar();
	return lidar_3d;
}

ptree get_2d_lidar()
{
    ptree lidar_2d = cloud_api->get_2d_lidar();
	return lidar_2d;
}

ptree get_camera()
{
    ptree camera = cloud_api->get_camera();
	return camera;
}

ptree get_sonar()
{
    ptree sonar = cloud_api->get_sonar();
    return sonar;
}
ptree get_infra()
{
    ptree infra = cloud_api->get_infra();
    return infra;
}
ptree get_key()
{
    ptree infra = cloud_api->get_key();
    return infra;
}
ptree get_gps()
{
    ptree gps = cloud_api->get_gps();
    return gps;
}
ptree get_odom()
{
    ptree odom = cloud_api->get_odom();
    return odom;
}
ptree get_imu()
{
    ptree info = cloud_api->get_imu();
    return info;
}
ptree get_rfid()
{
    ptree rfid = cloud_api->get_rfid();
    return rfid;
}
ptree get_uwb()
{
    ptree uwb = cloud_api->get_uwb();
    return uwb;
}

ptree set_safe_area(string r1,string r2,string r3)
{
    double r1_ = 0.6;//atof(r1.c_str());
    double r2_ = 0.7;//atof(r2.c_str());
    double r3_ = 0.8;//atof(r3.c_str());
    cloud_api->set_safe_area(r1_,r2_,r3_);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree set_init_point(char buf[])
{
    cloud_api->set_init_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree set_calibration_point(char buf[])
{
    cloud_api->set_calibration_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree set_work_point(char buf[])
{
    cloud_api->set_work_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree set_charge_point(char buf[])
{
    cloud_api->set_charge_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree set_navi_point(char buf[])
{
    cloud_api->set_navi_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree set_user_point(char buf[])
{
    cloud_api->set_user_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree set_navi_path(char buf[])
{
    cloud_api->set_navi_path(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree set_virtual_wall(char buf[])
{
    cloud_api->set_virtual_wall(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree set_virtual_wall_point(char buf[])
{
    cloud_api->set_virtual_wall_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree set_user_path(char buf[])
{
    cloud_api->set_user_path(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree set_user_path_point(char buf[])
{
    cloud_api->set_user_path_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree set_slow_area(char buf[])
{
    cloud_api->set_slow_area(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree set_slow_area_point(char buf[])
{
    cloud_api->set_slow_area_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree set_stop_area(char buf[])
{
    cloud_api->set_stop_area(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree set_stop_area_point(char buf[])
{
    cloud_api->set_stop_area_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree set_user_area(char buf[])
{
    cloud_api->set_user_area(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree set_user_area_point(char buf[])
{
    cloud_api->set_user_area_point(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree set_mapping_mode(string map_name, string mode)
{
    cloud_api->set_mapping_mode(map_name, mode);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree start_scan()
{
    cloud_api->start_scan();
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree cancel_scan()
{
    cloud_api->cancel_scan();
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree finish_scan()
{
    cloud_api->finish_scan();
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree get_scan_status()
{
    ptree pt = cloud_api->get_scan_status();
    return pt;
}
ptree start_mapping()
{
    cloud_api->start_mapping();
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree cancel_mapping()
{
    cloud_api->cancel_mapping();
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree get_mapping_status()
{
    string msg;
    cloud_api->get_mapping_status(msg);
    ptree info;
    info.put<string>("msg",msg, string_translator<std::string>());
    return info;
}

ptree global_position()
{
    cloud_api->global_position();
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree initial_position(char buf[])
{
    cloud_api->initial_position(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}

ptree start_route(char buf[])
{
    cloud_api->start_route(buf);
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree get_running_status()
{
   ptree info = cloud_api->get_running_status();
   return info; 
}
ptree pause_task()
{
    cloud_api->pause_task();
    ptree info;
    set_succeed_return(info);
    return info;
}
ptree resume_task()
{
    cloud_api->resume_task();
    ptree info;
    set_succeed_return(info);
    return info;
}
